package com.yzkj.cpjxc.business.system.listener;

import com.yzkj.cpjxc.business.framework.utils.ContextUtils;
import com.yzkj.cpjxc.business.service.SysAuthorityService;
import com.yzkj.cpjxc.business.service.SysDeptService;
import com.yzkj.cpjxc.business.service.SysRoleService;
import com.yzkj.cpjxc.business.service.SysUserService;
import com.yzkj.cpjxc.business.system.interceptor.PermissionInterceptor;
import org.apache.log4j.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SysInitListener implements ServletContextListener {
	
	private Logger log = Logger.getLogger(PermissionInterceptor.class);

	public void contextDestroyed(ServletContextEvent contextEvent) {

	}

	public void contextInitialized(ServletContextEvent event) {
		ServletContext application = event.getServletContext();
		
		// 全局上下文路径
		application.setAttribute("ctx", application.getContextPath());
		
		// 配置log4j日志的路径
		System.setProperty("log4jdir",application.getRealPath("/"));
		
		try {
			//初始化角色
			SysRoleService sysRoleService = (SysRoleService) ContextUtils.getBean("sysRoleService");
			sysRoleService.initData();
			
			//初始化权限
			SysAuthorityService sysAuthorityService = (SysAuthorityService)ContextUtils.getBean("sysAuthorityService");
			sysAuthorityService.initData();
			
			//初始化顶级部门
			SysDeptService sysDeptService = (SysDeptService)ContextUtils.getBean("sysDeptService");
			sysDeptService.initData();
			
			//初始化超级管理员
			SysUserService sysUserService = (SysUserService)ContextUtils.getBean("sysUserService");
			sysUserService.initData();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
